import ResponseHandler from "./responseHandler.js";

const asyncHandler = fn => (req, res, next) => {
    Promise.resolve(fn(req, res, next))
        .catch(async err => {
            // 记录错误日志
            if (err.status) {
                await ResponseHandler.logResponse(err.status, err.message);
                await ResponseHandler.responseResult(res, null, err.message); // 使用错误状态码
            } else {
                await ResponseHandler.logResponse(500, err.message);
                await ResponseHandler.responseResult(res, null, '内部服务器错误'); // 默认返回500
            }
        });
};

export default asyncHandler;
